package com.zadtek.shopmall.activities;

import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.fragments.CarFragment;
import com.zadtek.shopmall.fragments.TabFragment;
import com.zadtek.shopmall.fragments.TabFragment2;
import com.zadtek.shopmall.fragments.TabFragment3;
import com.zadtek.shopmall.fragments.TabFragment4;

import java.util.LinkedList;
import java.util.List;

public class CarActivity extends AbsActivity {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private ImageView icBack;
    private List<String> titles = new LinkedList<>();
    private List<Fragment> fragments = new LinkedList<>();
    @Override
    public int setView() {
        return R.layout.activity_car;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initData();
        initView();
        initLogic();
    }

    private void initData() {
        titles.add("人找车");
        titles.add("车找人");
        titles.add("货找车");
        titles.add("车找货");
        fragments.add(new TabFragment());
        fragments.add(new TabFragment2());
        fragments.add(new TabFragment3());
        fragments.add(new TabFragment4());
    }

    private void initLogic() {
        icBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        viewPager.setAdapter(new CarAdapter(getSupportFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);
    }

    private void initView() {
        viewPager = findViewById(R.id.viewpager);
        tabLayout = findViewById(R.id.tablayout);
        icBack = findViewById(R.id.ic_back);
    }

    private class CarAdapter extends FragmentPagerAdapter {
        public CarAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }
}
